//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
	phone: '',
  },
  onLoad: function () {
    var key = wx.getStorageSync('key')
    if (app.globalData.userInfo && key) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      wx.reLaunch({
        url: '../member/member',
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        this.get_bangding(res)
      }

    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    if (e.detail.userInfo) {
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      if (e.detail.userInfo) {
        this.get_bangding(e.detail)
      }
    } else {
      wx.reLaunch({
        url: '../member/member',
      })
    }


  },
  getPhoneNumber: function (e) {
      // console.log(e.detail.iv);
      // console.log(e.detail.encryptedData);
      wx.login({
        success: res => {
          console.log(res.code);
			var session_key = wx.getStorageSync('session_key')
			var that = this
          wx.request({
            url: app.configData.ucenter.api_url + "/index.php?ctl=Connect_Wxapp&met=phoneLogin",
            
            data: { appid: app.data.appid, code: res.code, encryptedData: e.detail.encryptedData, iv: e.detail.iv, type:2 },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: {	
              'content-type': 'application/json'
            }, // 设置请求的 header
            success: function (res) {
          		console.log(res)
				wx.setStorage({
				  key: 'unionId',
				  data: res.data.openid,
				})
				var unionId = res.data.openid;
				if (!unionId) {
				  app.onLaunch()
				  return false
				}
				var phone = res.data.phoneNumber
				//手机号码
				that.setData({
				  phone: phone
				})
				wx.setStorage({
				  key: 'phone',
				  data: phone
				})
				var userInfo = res.data.userInfo;
				wx.request({
				  url: app.configData.ucenter.api_url + "/index.php?ctl=Connect_Wxapp&met=wx_app&typ=json",
				  data: { openid: unionId, token: unionId, userInfo:  userInfo},
				  method: 'GET',
				  success: function (resz) {
				    console.log(resz)
				    if (resz.data.status == 200) {
				      wx.setStorage({
				        key: 'key',
				        data: resz.data.data.k
				      })
				      wx.setStorage({
				        key: 'u',
				        data: resz.data.data.u
				      })
				      setTimeout(function () {
				        wx.navigateBack();
				      }, 500)
				    } else {
				      wx.redirectTo({
				        url: '../select/select?unionId=' + unionId,
				      })
				    }
				  }
				})
				
            },
            fail: function (err) {
              console.log(err);
            }
          })
        }
      })
    },
  onBrowseWithoutLogin: function() {
	  wx.switchTab({
	    url: '/pages/index/index',
	  })
  },
  //检测用户是否授权绑定账号
  get_bangding: function (info) {

    var that = this
    var session_key = wx.getStorageSync('session_key')
    wx.request({
      url: app.configData.ucenter.api_url + "/index.php?ctl=Connect_Wxapp&met=getUnionid",
      data: { appid: app.data.appid, session_key: session_key, encryptedData: info.encryptedData, iv: info.iv, type:2 },
      method: 'GET',
      success: function (res) {
        if (res.statusCode == 200) {
          wx.setStorage({
            key: 'unionId',
            data: res.data.openId,
          })
          var unionId = res.data.openId;
          if (!unionId) {
            app.onLaunch()
            return false
          }
          wx.request({
            url: app.configData.ucenter.api_url + "/index.php?ctl=Connect_Wxapp&met=wx_app&typ=json",
            data: { openid: unionId, token: unionId, userInfo: info.userInfo },
            method: 'GET',
            success: function (resz) {
              console.log(resz)
              if (resz.data.status == 200) {
                wx.setStorage({
                  key: 'key',
                  data: resz.data.data.k
                })
                wx.setStorage({
                  key: 'u',
                  data: resz.data.data.u
                })
                setTimeout(function () {
				  wx.navigateBack();
                }, 500)
              } else {
                wx.redirectTo({
                  url: '../select/select?unionId=' + unionId,
                })
              }
            }
          })
        }

      }
    })
  }
})
